package fr.jade.fraclite.type;

import java.lang.reflect.Array;

import org.objectweb.fractal.api.factory.InstantiationException;
import org.objectweb.fractal.api.type.ComponentType;
import org.objectweb.fractal.api.type.InterfaceType;
import org.objectweb.fractal.api.type.TypeFactory;
import org.objectweb.fractal.deployment.local.api.ITypeFactoryWithModules;

/**
 * Provides a basic implementation of the {@link TypeFactory} interface. <br>
 * <br>
 * <b>Requirements</b>
 * <ul>
 * <li>none.</li>
 * </ul>
 */

public class BasicTypeFactoryWithModules extends BasicTypeFactory implements ITypeFactoryWithModules {

	public ComponentType createFcType(InterfaceType[] interfaceTypes)
			throws InstantiationException {

		boolean hasModuleItf = false;
		for (int i = 0; i < interfaceTypes.length; i++) {
			InterfaceType itfType = interfaceTypes[i];
			if (itfType.getFcItfSignature().equals(
					"org.objectweb.fractal.deployment.local.api.IModule")) {
				hasModuleItf = true;
				break;
			}
		}

		if (!hasModuleItf) {
			InterfaceType[] newInterfaceTypes = (InterfaceType[]) Array
					.newInstance(InterfaceType.class, interfaceTypes.length + 1);
			System.arraycopy(interfaceTypes, 0, newInterfaceTypes, 0,
					interfaceTypes.length);
			InterfaceType moduleItf = super.createFcItfType("module",
					"org.objectweb.fractal.deployment.local.api.IModule", true,
					false, false);
			newInterfaceTypes[newInterfaceTypes.length-1] = moduleItf;
			interfaceTypes = newInterfaceTypes;
		}
		return new BasicComponentType(interfaceTypes);
	}

	public ComponentType createFcMembraneType(InterfaceType[] interfaces) throws InstantiationException {
		return super.createFcType(interfaces);
	}
}
